package com.lj.demo.ucenterService.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lj.demo.commonutils.JwtUtils;
import com.lj.demo.commonutils.MD5;
import com.lj.demo.servicebase.exception.AException;
import com.lj.demo.ucenterService.entity.UcenterMember;
import com.lj.demo.ucenterService.entity.vo.RegisterVo;
import com.lj.demo.ucenterService.mapper.UcenterMemberMapper;
import com.lj.demo.ucenterService.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author liujiang
 * @since 2020-12-23
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    //会员登录
    @Override
    public String login(UcenterMember ucenterMember) {
        String mobile = ucenterMember.getMobile();
        String password = ucenterMember.getPassword();

        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)){
            throw new AException(20001,"请输入手机号和密码");
        }

        //获取会员
        UcenterMember member = baseMapper.selectOne(new QueryWrapper<UcenterMember>().eq("mobile", mobile));
        if (member == null) throw new AException(20001,"登录手机号不正确");

        //校验密码
        if (!MD5.encrypt(password).equals(member.getPassword())){
            throw new AException(20001,"密码不正确");
        }

        //校验是否被禁用
        if(member.getIsDisabled()) {
            throw new AException(20001,"手机号被禁用");
        }

        //使用jwt生成token字符串
        String token = JwtUtils.getJwtToken(member.getId(), member.getNickname());
        return token;
    }


    //会员注册
    @Override
    public void register(RegisterVo registerVo) {
        //获取注册信息，进行校验
        String nickname = registerVo.getNickname();
        String mobile = registerVo.getMobile();
        String password = registerVo.getPassword();
        String code = registerVo.getCode();

        //校验参数
        if(StringUtils.isEmpty(nickname) ||
                StringUtils.isEmpty(mobile) ||
                StringUtils.isEmpty(password) ||
                StringUtils.isEmpty(code)) {
            throw new AException(20001,"请输入正确的注册信息");
        }

        //模拟添加验证码
        redisTemplate.opsForValue().set(mobile,"8888",2, TimeUnit.MINUTES);

        //从Redis中获取校验码
        String mobileCode = redisTemplate.opsForValue().get(mobile);
        if (!code.equals(mobileCode)) throw new AException(20001,"验证码不正确");

        //查询数据库中是否有次手机号
        Integer count = baseMapper.selectCount(new QueryWrapper<UcenterMember>().eq("mobile", mobile));
        if (count.intValue() > 0) throw new AException(20001,"此手机号已存在");

        //添加注册信息到数据库
        UcenterMember member = new UcenterMember();
        member.setNickname(nickname);
        member.setMobile(registerVo.getMobile());
        member.setPassword(MD5.encrypt(password));
        member.setIsDisabled(false);
        member.setAvatar("https://edu-ljj1002.oss-cn-beijing.aliyuncs.com/2020/11/29/9e24053f-af2b-44e0-9278-b1e625be4046file.png");
        this.save(member);
    }

    //统计某一天的注册人数
    @Override
    public Integer countRegister(String day) {
        return baseMapper.selectRegisterCount(day);
    }


}
